﻿using System;
using WorkOrder.Model;

namespace WorkOrder.Business
{
    public class Business_Table_Permission
    {
        DbHelper dbhelper = new DbHelper();

        #region 保存
        public bool Insert(Model_Table_Permission model)
        {
            string sqlstr = " insert into Table_Permission(user_id,table_name,permission_Insert,permission_Delete,permission_Modify,permission_Query,remark) values(";
            sqlstr += model.User_id + ",'" + model.Table_name + "'," + (model.Permission_Insert ? 0 : 1).ToString() + "," + (model.Permission_Delete ? 0 : 1).ToString() + ","
                + (model.Permission_Modify ? 0 : 1).ToString() + "," + (model.Permission_Query ? 0 : 1).ToString() + ",'" + model.Remark + "'";
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 更新
        public bool Update(Model_Table_Permission model)
        {
            string sql = "update Table_Permission set permission_Insert=" + (model.Permission_Insert ? 0 : 1).ToString()
                + ",permission_Delete=" + (model.Permission_Delete ? 0 : 1).ToString()
                + ",permission_Modify=" + (model.Permission_Modify ? 0 : 1).ToString()
                + ",permission_Query=" + (model.Permission_Query ? 0 : 1).ToString()
                + ", remark='" + model.Remark + "' where permission_ID=" + model.Permission_ID + " and user_id=" + model.User_id + " and table_name='" + model.Table_name + "'";
            if (dbhelper.ExecuteNoQuery(sql) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 得到单个数据集
        public Model_Table_Permission GetData(string keyValue)
        {
            string sql = "select * from Table_Permission where routing_id=" + keyValue;
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sql))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_Permission tmp;
                    tmp = new Model_Table_Permission(Convert.ToInt32(rdr["permission_ID"]), Convert.ToInt32(rdr["user_id"]), rdr["table_name"].ToString(), (rdr["permission_Insert"].ToString() == "0" ? false : true),
                        (rdr["permission_Delete"].ToString() == "0" ? false : true), (rdr["permission_Modify"].ToString() == "0" ? false : true), (rdr["permission_Query"].ToString() == "0" ? false : true),
                        rdr["remark"].ToString());
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else
                    return null;
            }
        }
        #endregion

        #region 根据条件得到数据集
        public System.Data.DataSet GetDatas(string condition, int pageSize, int currentPage, string orderField, OrderType ot, string userID)
        {
            string sql = "UP_GET_PAGEINFO 'Table_Permission','*"
                     + "','" + condition + "','','" + orderField + "'," + (ot == OrderType.ASC ? 0 : 1) + "," + pageSize + "," + currentPage;
            return dbhelper.Query(sql);
        }
        #endregion

        public bool HasInsert(string user_id, string table_name)
        {
            string sqlstr = "select permission_Insert from Table_Permission where user_id=" + user_id + " and table_name='" + table_name + "'";
            object ob = dbhelper.ExecuteScalar(sqlstr);
            if (ob != null && Convert.ToBoolean(ob))
                return true;
            else
                return false;
        }

        public bool HasDelete(string user_id, string table_name)
        {
            string sqlstr = "select permission_Delete from Table_Permission where user_id=" + user_id + " and table_name='" + table_name + "'";
            object ob = dbhelper.ExecuteScalar(sqlstr);
            if (ob != null && Convert.ToBoolean(ob))
                return true;
            else
                return false;
        }

        public bool HasModify(string user_id, string table_name)
        {
            string sqlstr = "select permission_Modify from Table_Permission where user_id=" + user_id + " and table_name='" + table_name + "'";
            object ob = dbhelper.ExecuteScalar(sqlstr);
            if (ob != null && Convert.ToBoolean(ob))
                return true;
            else
                return false;
        }

        public bool HasQuery(string user_id, string table_name)
        {
            string sqlstr = "select permission_Query from Table_Permission where user_id=" + user_id + " and table_name='" + table_name + "'";
            object ob = dbhelper.ExecuteScalar(sqlstr);
            if (ob != null && Convert.ToBoolean(ob))
                return true;
            else
                return false;
        }
    }
}
